package com.lframework.xingyun.basedata.impl.make;

import cn.hutool.core.collection.CollUtil;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.core.utils.IdUtil;
import com.lframework.xingyun.basedata.dto.make.MakeMethodMaterialDto;
import com.lframework.xingyun.basedata.entity.MakeMethod;
import com.lframework.xingyun.basedata.entity.MakeMethodMaterial;
import com.lframework.xingyun.basedata.mappers.MakeMethodMaterialMapper;
import com.lframework.xingyun.basedata.service.make.MakeMethodMaterialService;
import com.lframework.xingyun.basedata.service.make.MakeMethodService;
import com.lframework.xingyun.basedata.vo.make.MaterialsVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class MakeMethodMaterialServiceImpl extends BaseMpServiceImpl<MakeMethodMaterialMapper, MakeMethodMaterial> implements MakeMethodMaterialService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(MakeMethod makeMethod, List<MaterialsVo> vos) {
        if(CollUtil.isNotEmpty(vos)) {
            String makeId = makeMethod.getId();
            lambdaUpdate().eq(MakeMethodMaterial::getMakeId, makeId).remove();
            List<MakeMethodMaterial> addBath = new ArrayList<>();

            for (int i = 0; i < vos.size(); i++) {
                MaterialsVo mainVo = vos.get(i);
                MakeMethodMaterial makeMethodMaterial = new MakeMethodMaterial();
                makeMethodMaterial.setId(IdUtil.getId());
                makeMethodMaterial.setMakeId(makeId);
                makeMethodMaterial.setType(mainVo.getType());
                makeMethodMaterial.setCode(mainVo.getCode());
                makeMethodMaterial.setSkuCode(mainVo.getSkuCode());
                makeMethodMaterial.setConsumeUnitId(mainVo.getConsumeUnitId());
                makeMethodMaterial.setConsumeNum(mainVo.getConsumeNum());
                makeMethodMaterial.setConsumeCost(mainVo.getConsumeCost());
                makeMethodMaterial.setOrderNo(i + 1);
                addBath.add(makeMethodMaterial);

                List<MaterialsVo> replacements = mainVo.getReplacements();
                if (CollUtil.isNotEmpty(replacements)) {
                    for (int i1 = 0; i1 < replacements.size(); i1++) {
                        MaterialsVo materialsVo = replacements.get(i1);
                        MakeMethodMaterial replacementMaterial = new MakeMethodMaterial();
                        replacementMaterial.setMakeId(makeId);
                        replacementMaterial.setType(materialsVo.getType());
                        replacementMaterial.setCode(materialsVo.getCode());
                        replacementMaterial.setSkuCode(materialsVo.getSkuCode());
                        replacementMaterial.setConsumeUnitId(materialsVo.getConsumeUnitId());
                        replacementMaterial.setConsumeNum(materialsVo.getConsumeNum());
                        replacementMaterial.setConsumeCost(materialsVo.getConsumeCost());
                        replacementMaterial.setOrderNo(i1 + 1);
                        replacementMaterial.setMainCode(makeMethodMaterial.getCode());
                        replacementMaterial.setMainSkuCode(makeMethodMaterial.getSkuCode());
                        addBath.add(replacementMaterial);
                    }
                }
            }
            saveBatch(addBath);

            getThis(MakeMethodService.class).lambdaUpdate()
                    .set(MakeMethod::getRelationMaterials, addBath.size())
                    .eq(MakeMethod::getId, makeMethod.getId())
                    .update();
        }
    }

    @Override
    public List<MakeMethodMaterialDto> selectDtoByMakeId(String makeId) {
        return getBaseMapper().selectDtoByMakeId(makeId);
    }

    @Override
    public List<MakeMethodMaterial> selectByCodeAndSkuCode(String code, String skuCode) {
        return getBaseMapper().selectByCodeAndSkuCode(code, skuCode);
    }

}
